package com.wmyg.scanninggun.utils;

import com.wmyg.scanninggun.bean.Bill;
import com.wmyg.scanninggun.bean.BillNew;
import com.wmyg.scanninggun.message.BillRequest;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class BillConverter {

    public static Bill billRequest2Bill(BillRequest billRequest) {
        return new Bill(billRequest);
    }

    public static BillNew billRequest2BillNew(BillRequest billRequest) {
        BillNew billNew = new BillNew();
        billNew.setOperator(billRequest.getOperator());
        billNew.setDeviceId(billRequest.getDeviceId());
        try {
            billNew.setCreatime(new SimpleDateFormat("yyyyMMddHHmmssSSS").parse(billRequest.getCreatime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        billNew.setGtin(billRequest.getCaseId().substring(4, 18));
        billNew.setBatch(billRequest.getCaseId().substring(22, 28));
        billNew.setCaseId(billRequest.getCaseId().substring(32));
        String[] boxes = new String[billRequest.getBoxes().length];
        for (int i=0;i<billRequest.getBoxes().length;i++) {
            boxes[i] = billRequest.getBoxes()[i].substring(32);
        }
        billNew.setBoxes(boxes);
        return billNew;
    }

    public static Bill billNew2Bill(BillNew billNew) {
        Bill bill = new Bill();
        bill.setBillId(billNew.getBillId());
        bill.setOperator(billNew.getOperator());
        bill.setDeviceId(billNew.getDeviceId());
        bill.setCaseId("(01)" + billNew.getGtin() + "(10)" + billNew.getBatch() + "(21)" + billNew.getCaseId());
        bill.setState(billNew.getState());
        bill.setCreatime(billNew.getCreatime());
        bill.setFactoryId(billNew.getFactoryId());
        bill.setFactoryName(billNew.getFactoryName());
        String[] boxes = new String[billNew.getBoxes().length];
        int i = 0;
        for (String box :
                billNew.getBoxes()) {
            boxes[i] = box;
        }
        bill.setBoxes(boxes);
        return bill;
    }

}
